using System;
using System.Data;
using System.Collections.Generic;
using rlTodayMakeSkin.Project;

namespace rlTodayMakeSkin.GUI
{
    /// <summary>
    /// Manages current project
    /// </summary>
    class ProjectsManager
    {
        IDisplayProject display;
        Dictionary<int, rlTodayProject> projects;
        rlTodayProject current;


        public ProjectsManager(IDisplayProject dp)
        {
            display = dp;
            projects = new Dictionary<int, rlTodayProject>();
            
        }

        /// <summary>
        /// Add a project to the manager
        /// </summary>
        /// <param name="path">path of directory of the project</param>
        /// <returns>a reference to just created project</returns>
        /// <remarks>Then name of the proejct is the name of its directory</remarks>
        public rlTodayProject addProject(string path)
        {
            string name = path.Substring(path.LastIndexOf("\\") + 1); //the name of the project is the name of its directory
            rlTodayProject prj = new rlTodayProject(path, display.getSkinDisplay());
            projects.Add(name.GetHashCode(), prj);
            return prj;
        }

        /// <summary>
        /// Close specified project
        /// </summary>
        /// <param name="name">Name of the project to close</param>
        /// <remarks>If name of the project is that of the current project
        /// it will be set to null</remarks>
        public void closeProject(string name)
        {
            rlTodayProject p = projects[name.GetHashCode()];
            display.closingProject(p);
            if (current.Name == name) current = null;
            projects.Remove(name.GetHashCode());
        }

        /// <summary>
        /// Close current project
        /// </summary>
        public void closeCurrentProject()
        {
            if(current != null)
                closeProject(current.Name);
        }


        /// <summary>
        /// Retrives project with this name
        /// </summary>
        /// <param name="name">name of the project to get</param>
        /// <returns>Found project or null if it is not found</returns>
        public rlTodayProject getProject(string name)
        {
            rlTodayProject p = null;
            try
            {
                p = projects[name.GetHashCode()];
            }
            catch (KeyNotFoundException knf) { }
            return p;
        }

        /// <summary>
        /// sets a project as current
        /// </summary>
        /// <param name="name">name of the project to active</param>
        /// <returns>returns current project. Null if is not found</returns>
        public rlTodayProject activeProject(string name)
        {
            try
            {
                current = projects[name.GetHashCode()];
                display.showProject(current);
            }
            catch (KeyNotFoundException e) { current = null; }
            return current;
        }

        /// <summary>
        /// Gets project currently active
        /// </summary>
        /// <returns></returns>
        public rlTodayProject getCurrentProject()
        {
            return current;
        }

        /// <summary>
        /// Creates a new project from file
        /// </summary>
        /// <param name="path">path of the projcet</param>
        /// <returns>Project just created</returns>
        public rlTodayProject loadProject(string path)
        {
            rlTodayProject p = new rlTodayProject(path, display.getSkinDisplay());
            p.loadSkin(path);
            projects.Add(p.Name.GetHashCode(), p);
            return p;
        }

        /// <summary>
        /// Find the name of the actual key in the RegistryKey.xml file and if found
        /// returns it. Returns "" otherwise
        /// </summary>
        /// <param name="name">Name of the key</param>
        /// <returns>Actual key associated by this name</returns>
        public static string GetRegKeyValue(string name){
            DataSet ds = new DataSet();
            DataRow[] dra = null;
            ds.ReadXml(System.Windows.Forms.Application.StartupPath + @"\rtrdk\RegistryKeys.xml");
            dra = ds.Tables[0].Select("Name='" + name + "'");
            if (dra.Length == 0) return "";
            return dra[0]["Key"].ToString();
        }

        /// <summary>
        /// Searches the keyvalue in the RegistryKey.xml file and if found it
        /// returns its name. Otherwise returns ""
        /// </summary>
        /// <param name="keyValue">value of the key</param>
        /// <returns></returns>
        public static string GetNameKeyFromValue(string keyValue)
        {
            DataSet ds = new DataSet();
            DataRow[] dra = null;
            ds.ReadXml(System.Windows.Forms.Application.StartupPath + @"rtrdk\RegistryKeys.xml");
            dra = ds.Tables[0].Select("Key='" + keyValue + "'");
            if (dra.Length == 0) return "";
            return dra[0]["Name"].ToString();
        }
    }
}
